using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using WebControls;
using InfraStructureLayer;
using EntityLayer;
using BusinessLayer;
using System.Text.RegularExpressions;

public partial class searchResults : SFPage
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected bool sfrSearchResults_NeedsDataSource()
    {
        entSearch insEntSearch = new entSearch();
        insEntSearch.SearchText = Session["SearchText"].ToString();
        insEntSearch.SeachExactMatches = chkSearchExactMatches.Checked;
        if (chkSearchAllLanguages.Checked)
        {
            insEntSearch.LanguageId = null;
        }
        else
        {
            insEntSearch.LanguageId = CurrentContext.LanguageId;
        }
        busSearch insBusSearch = new busSearch();
        DataTable insDataTableSearch = insBusSearch.Search(insEntSearch);
        if (insBusSearch.HasErrors)
        {
            MessageBoxShow(insBusSearch);
            return true;
        }
        sfrSearchResults.SetData(insDataTableSearch);
        txtSearch.Text = Session["SearchText"].ToString();
        return insBusSearch.HasErrors;
    }
    protected string GenerateUrl(object address, object Id)
    {
        if (IsGuid(Id.ToString()))
        {
            return address.ToString() + Id.ToString() + ".aspx?q=" + (chkSearchExactMatches.Checked ? "_" : "") + Session["SearchText"].ToString() + (chkSearchExactMatches.Checked ? "_" : "");
        }
        else
        {
            return address.ToString() + new Crypto().EncryptURL(Id.ToString()) + ".aspx?q=" + (chkSearchExactMatches.Checked ? "_" : "") + Session["SearchText"].ToString() + (chkSearchExactMatches.Checked ? "_" : "");
        }
    }
    private bool IsGuid(string s)
    {
        Regex regExp = new Regex(@"^\{?[a-fA-F\d]{8}-([a-fA-F\d]{4}-){3}[a-fA-F\d]{12}\}?$");

        return regExp.IsMatch(s);
        
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        if (txtSearch.Text != "")
        {
            Session["SearchText"] = txtSearch.Text;
            sfrSearchResults.LoadRepeaterData();
        }
    }
    protected string GetImageUrl(object url)
    {
        return "~/images/SearchResults/" + url.ToString() + ".gif";
    }
}
